package cz.muni.fi.pa036is.servicesimplementation;

import cz.muni.fi.pa036is.daointerface.MatchDAO;
import cz.muni.fi.pa036is.daointerface.RoundDAO;
import cz.muni.fi.pa036is.dto.MatchBasicTO;
import cz.muni.fi.pa036is.dto.RoundFormTO;
import cz.muni.fi.pa036is.dto.RoundGridTO;
import cz.muni.fi.pa036is.dtoconverters.MatchConverter;
import cz.muni.fi.pa036is.dtoconverters.RoundConverter;
import cz.muni.fi.pa036is.entity.Match;
import cz.muni.fi.pa036is.entity.Round;
import cz.muni.fi.pa036is.servicesinterface.RoundService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Shebik
 */
@Service
public class RoundServiceImpl implements RoundService {

    @Autowired
    private RoundDAO roundDAO;

    @Autowired
    private MatchDAO matchDAO;

    @Override
    public void addRound(final RoundFormTO roundFormTO) {   //todo - doriesit sekvencne poradie
        
        Round round = RoundConverter.convertToRound(roundFormTO);
        
        roundDAO.addRound(round);
    }

    @Override
    public void editRound(final RoundFormTO roundFormTO) {   //todo - doriesit sekvencne poradie
        roundDAO.editRound(RoundConverter.convertToRound(roundFormTO));
    }

    @Override
    public void deleteRound(final Long matchId, int sequenceNumber) {
        roundDAO.deleteRound(matchId, sequenceNumber);                            //todo - doriesit sekvencne poradie
    }

    @Override
    public RoundFormTO getRoundForm(Long matchId, int sequenceNumber) {
        final Round round = roundDAO.findRound(matchId, sequenceNumber);
        final RoundFormTO roundFormTO = RoundConverter.convertToRoundFormTO(
                round);
        final MatchBasicTO matchBasicTO = MatchConverter.convertToMatchBasicTO(
                matchDAO.findMatch(matchId));
        roundFormTO.setMatch(matchBasicTO);
        return roundFormTO;
    }

    @Override
    public List<RoundGridTO> getAllRoundsForMatch(final Long matchId) {
        return RoundConverter.convertToRoundGridTOList(
                roundDAO.getAllRoundsForMatch(matchId));
    }

				@Override
				public int getNextRoundIndexForMatch(Long id) {
								return this.roundDAO.getNextIndexForMatch(id);
				}

}
